package com.rainbow.service.fallback;

import com.rainbow.domain.Product;
import com.rainbow.service.ProductService;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * feigin+sentinel 容错类 + 异常处理
 */
@Component
@Slf4j
public class ProductServiceFallbackFactory implements FallbackFactory<ProductService> {

    @Override
    public ProductService create(Throwable throwable) {
        return new ProductService() {
            @Override
            public Product findByPid(Integer pid) {
                log.info("feigin error");
                log.error("product findByPid {}",throwable);
                // 容错逻辑
                Product product = new Product();
                product.setPid(-100);
                product.setPname("容错类 pname");
                // todo rollback
                return product;
            }

            @Override
            public String sale(Integer pid, Integer number) {
                log.error("product sale {}",throwable);
                // todo rollback
                return "sale error";
            }
        };
    }
}
